package tesseract;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.awt.image.BufferedImage;
import java.io.File;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 14:47:52 2018/11/13
 * @version:2.0
 */
public class Test {
    public static void main(String[] args) {
        //图片所在文件夹
        testTess4j("验证码");//src\main\resources\test-data
    }

    public static void testTess4j(String filePath) {
        File root = new File(filePath);
        ITesseract instance = new Tesseract();
        instance.setDatapath("src\\main\\resources\\tessdata");
        instance.setLanguage("chi_sim");
        try {
            File[] files = root.listFiles();
            for (File file : files) {
                String result = instance.doOCR(file);
                String fileName = file.toString().substring(file.toString().lastIndexOf("\\") + 1);
                System.out.println("图片名：" + file.toString() + " 识别结果：" + result);
            }
        } catch (TesseractException e) {
            System.err.println(e.getMessage());
        }
    }

    public static String getResult(File file) {
        ITesseract instance = new Tesseract();
        instance.setDatapath("src\\main\\resources\\tessdata");
        instance.setLanguage("chi_sim");
        try {
            String result = instance.doOCR(file);
            return result;
        } catch (TesseractException e) {
            //e.printStackTrace();
        }
        return null;
    }

    public static String getResultBufferedImage(BufferedImage file) {
        ITesseract instance = new Tesseract();
        instance.setDatapath("src\\main\\resources\\tessdata");
        instance.setLanguage("chi_sim");
        try {
            String result = instance.doOCR(file);
            return result;
        } catch (TesseractException e) {
            e.printStackTrace();
        }
        return null;
    }
}
